<?php

class Post{

	var $db;

	public function __construct(){
		$this->db = new DBClass();
	}
	
	public function get_all_post(){
		
		$query = "Select * from post p LEFT JOIN log_activity_post lp ON p.id_post = lp.post_id_post LEFT JOIN user u ON lp.user_id_user = u.id_user LEFT JOIN kategori k ON p.kategori_id_kategori = k.id_kategori";

		$rows = $this->db->getRows($query);

		return $rows;	

	}

	public function get_all_tag($id){
		$query = "SELECT pt.post_id_post, t.id_tag, t.jdl_tag from post_has_tag pt JOIN tag t ON pt.tag_id_tag=t.id_tag where post_id_post='".@$id."'";

		$rows = $this->db->getRows($query);
		return $rows;
	}

	public function addPost($data){
		
		$query ="INSERT INTO POST SET jdl_post='".@$data['jdl_post']."',
		isi_post='".@$data['isi_post']."',kategori_id_kategori='".@$data['kategori']."',
		gambar_post='".@$data['gambar_post']."',tgl_rilis_post='".date('Y-m-d')."'";
		$rows = $this->db->getRows($query);

		$lastID = $this->db->insertId();
		foreach ($data['tag'] as $value) {
			$query ="INSERT INTO post_has_tag SET post_id_post='".$lastID."',
			tag_id_tag='".@$value."'";
			$rows = $this->db->getRows($query);
		}
		

		$query ="INSERT INTO log_activity_post SET post_id_post='".$lastID."',
		datetime_activity='".date('Y-m-d')."',user_id_user='".@$_COOKIE['c_id_user']."'";
		$rows = $this->db->getRows($query);

		return $lastID;
	}

	public function deletePost($data){
		
		$query ="DELETE FROM post WHERE id_post='".@$data."'";

		$rows = $this->db->getRows($query);

		header('Location: post.php');
	}

	public function insertImage($id, $imageName) {
		$query ="UPDATE post SET gambar_=post'".@$imageName."' WHERE id_user='".$id."'";

		$rows = $this->db->putRows($query);

		return $rows;
	}


}